import pytest
import requests

from api.invest_api import InvestApi
from api.reg_login_api import RegLoginApi
from common.util import build_account, html_parser, init_tender_info
from config import path_cfg


class TestInvest(object):

    def setup_class(self):
        init_tender_info()

    def setup(self):
        self.session = requests.Session()
        self.reg_Login_api = RegLoginApi(self.session)
        self.invest_api = InvestApi(self.session)

        phone = path_cfg.REG_LOGIN_PHONE
        build_account('invest', self.session, phone)
        self.reg_Login_api.user_login({"keywords": phone, "password": "a123456"})

    def teardown(self):
        self.session.close()

    # 投资
    @pytest.mark.run(order=1)
    def test_invest(self):
        form_data = {
            "id": 642,
            "depositCertificate": -1,
            "amount": 500
        }
        resp = self.invest_api.invest(form_data)
        assert 200 == resp.status_code
        assert 200 == resp.json().get('status')

    # 第三方投资
    @pytest.mark.run(order=2)
    def test_third_invest(self):
        form_data = {
            "id": 642,
            "depositCertificate": -1,
            "amount": 500
        }
        invest_resp = self.invest_api.invest(form_data)

        form_data = invest_resp.json().get('description').get('form')

        parser_data = html_parser(form_data)

        third_invest_resp = self.invest_api.third_invest(parser_data.get('url'), parser_data.get('req_body'))
        assert 'OK' in third_invest_resp.text
